package cn.ybk.org.controller;

import cn.ybk.basic.util.AjaxResult;
import cn.ybk.basic.util.ExcelUtil;
import cn.ybk.basic.util.PageList;
import cn.ybk.org.domain.Shop;
import cn.ybk.org.dto.ShopAuditDto;
import cn.ybk.org.dto.ShopResultDto;
import cn.ybk.org.dto.ShopSettlementDto;
import cn.ybk.org.query.ShopQuery;
import cn.ybk.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;


    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }


    @GetMapping()
    public List<Shop> list() {
        return shopService.findAll();
    }

    // 查询全部
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryByPage(query);
    }

    // 店铺第一次入驻
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopSettlementDto dto) {
        shopService.settlement(dto);
        return AjaxResult.me().success();
    }

    // 审核通过
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody @Valid ShopAuditDto dto) {
        shopService.auditPass(dto);
        return AjaxResult.me().success();
    }

    // 审核驳回
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody @Valid ShopAuditDto dto) {
        shopService.auditReject(dto);
        return AjaxResult.me().success();
    }

    // 重新入驻
    @PostMapping("/settlement/again")
    public AjaxResult settlementAgain(@RequestBody @Valid ShopResultDto dto) {
        shopService.settlementAgain(dto);
        return AjaxResult.me().success();
    }

    // 激活店铺
    @GetMapping("/activity/{id}")
    public AjaxResult activity(@PathVariable("id") Long id) {
        shopService.activity(id);
        return AjaxResult.me().success();
    }

    @GetMapping("/export/excel")
    public void export(HttpServletResponse response) {
        // 获取数据
        List<Shop> all = shopService.findAll();
        // 调用util类 导出
        ExcelUtil.exportExcel(all, "", "店铺列表",
                Shop.class, "店铺列表.xlsx", response);

    }

    // 导入
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        // 调用util类 导入
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        // 将集合存入到数据库
        shops.forEach(System.out::println);
    }



}
